package init

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"zk2/user_server/basic/config"
)

func init() {
	InitViper()
	InitMysql()
}
func InitViper() {
	viper.SetConfigFile("../user_server/dev.yaml")
	viper.ReadInConfig()
	viper.Unmarshal(&config.Conf)
	log.Println("动态连接成功", config.Conf)
}
func InitMysql() {
	var err error
	a := config.Conf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", a.User, a.Password, a.Host, a.Post, a.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	log.Println("数据连接成功")
}
